package com.zk.app;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement;
import com.zk.config.SpringConfig;
import com.zk.domain.User;
import com.zk.mapper.UserCommonMapper;
import com.zk.mapper.UserMapper;
import jdk.nashorn.internal.objects.annotations.Setter;
import net.sf.jsqlparser.statement.execute.Execute;
import org.apache.avalon.framework.activity.Suspendable;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.List;

/**
 * @author cyt
 * @create 2021-03-19 0:59
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
public class MapperApplication {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private DruidDataSource druidDataSource;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserCommonMapper userCommonMapper;


    @Test
    public void testVersion() {
        //根据主键删除，没有指定版本时删除不了
        Assert.assertEquals(0, userCommonMapper.deleteByPrimaryKey(1));

        User user = new User();
        user.setVersion(0);
        user.setId(1);
        userCommonMapper.deleteByPrimaryKey(user);
    }


    @Test
    public void testExampleWeekend() {
        List<User> selectByWeekendSql = userCommonMapper.selectByExample(new Example.Builder(User.class)
                .where(WeekendSqls.<User>custom().andLike(User::getName, "%a%")
                        .andGreaterThan(User::getPwd, "123"))
                .build());

        selectByWeekendSql.forEach(System.out::println);
    }

    @Test
    public void testExampleBuilder() {
        Example name = Example.builder(User.class).orderByAsc("name").forUpdate().build();
        userCommonMapper.selectByExample(name).forEach(System.out::println);
    }

    @Test
    public void testExampleDistinct() {
        Example example = new Example(User.class);
        example.setDistinct(true);

        example.createCriteria().andLike("name", "11");
        example.or().andGreaterThan("id", 3);

        userCommonMapper.selectByExample(example).forEach(System.out::println);
    }


    @Test
    public void testExampleOrder() {
        Example example = new Example(User.class);
        example.orderBy("id").desc();
        userCommonMapper.selectByExample(example).forEach(System.out::println);


        example.clear();

        example.orderBy("name").asc().orderBy("pwd").desc();
        userCommonMapper.selectByExample(example).forEach(System.out::println);
    }

    @Test
    public void testDySql() {
        User user = new User();
        user.setName("chain");
        user.setPwd("chain");
        userCommonMapper.insert(user);

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();

        if (user.getName() != null) {
            criteria.andLike("name", user.getName() + "%");
        }
        List<User> users = userCommonMapper.selectByExample(example);
        users.forEach(System.out::println);
    }

    @Test
    public void testByExampleAndProperties() {
        Example example = new Example(User.class);
        example.and().andEqualTo("name", "11");
        List<User> users = userCommonMapper.selectByExample(example);
        users.forEach(System.out::println);
    }

    @Test
    public void testByExampleProperties() {
        Example example = new Example(User.class);
        example.selectProperties("name");
        List<User> users = userCommonMapper.selectByExample(example);
        users.forEach(System.out::println);
    }


    @Test
    public void testByExample() {
        Example example = new Example(User.class);
        example.setForUpdate(true);
        // example.createCriteria().andGreaterThan("id", 100).andLessThan("id", 151);
        example.or().andLessThan("id", 41);
        List<User> users = userCommonMapper.selectByExample(example);
        users.forEach(System.out::println);
    }

    @Test
    public void testCommonMapperConsumerMethods() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserCommonMapper mapper = sqlSession.getMapper(UserCommonMapper.class);
        System.out.println(mapper.selectByName("11"));
    }

    @Test
    public void testCommonMapperSelect() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserCommonMapper mapper = sqlSession.getMapper(UserCommonMapper.class);
        List<User> users = mapper.selectAll();
        users.forEach(System.out::print);

        System.out.println("\n");
        User x = mapper.selectByPrimaryKey(3);
        System.out.println(x);

        x.setPwd("update two");

        mapper.updateByPrimaryKey(x);
        System.out.println(x);
    }


    @Test
    public void testCommonMapperInsert() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserCommonMapper mapper = sqlSession.getMapper(UserCommonMapper.class);
        User user = new User();
        user.setName("commonMapper");
        user.setPwd("commonMapper");
        mapper.insert(user);
    }


    @Test
    public void test() {
        System.out.println(sqlSessionFactory);
        System.out.println(druidDataSource);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.insertUser("test", "test");
    }

}
